<template>
  <div class="btn_wrapper1">
  <el-card class="box-card">
   <div slot="header" class="clearfix">
    <div >
      <!-- 头部 -->
      <el-row class="btn_wrapper" type="flex">
        <el-col :span="21" class="btn-col">
          <span class="heardText">
            说明：目前支持学科和关键字条件筛选
          </span>
        </el-col>
        <el-col :span="2" class="btn-col1">
          <el-button class="el-icon-edit" @click="newFn" type="success">新增试题</el-button>
        </el-col>
      </el-row>
    </div>
    </div>
    <div class="text item">
         <!-- 表单部分 -->
      <el-form :inline="true" :model="userName" label-position="right" label-width="80px" class="demo-form-inline del">
        <el-form-item label="学科">
          <el-select size="mini" v-model="fromList.subjectId" @change='subject' placeholder="请选择">
            <el-option v-for="(item,index) in subjectArr" :key="index" :value="item.value" :label="item.label"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级目录">
          <el-select size="mini" v-model="fromList.catalog" placeholder="请选择">
            <el-option v-for="(item,index) in directoryArr" :key="index" :value="item.value" :label="item.label"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标签">
          <el-select size="mini" v-model="fromList.tags" placeholder="请选择">
            <el-option :value="fromList.tags"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关键字">
          <el-input v-model="fromList.keyword" size="mini" placeholder="根据题干搜索"></el-input>
        </el-form-item>
        <el-form-item label="试题类型">
          <el-select size="mini" v-model="fromList.questionType" placeholder="请选择">
            <el-option :label="item.label" :value="item.value" v-for="(item,index) in questionTypeArr" :key="index"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="难度">
          <el-select size="mini" v-model="fromList.difficulty" placeholder="请选择">
            <el-option :label="item.label" :value="item.value" v-for="(item,index) in difficultyArr" :key="index"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="方向">
          <el-select size="mini" v-model="fromList.direction" placeholder="请选择">
            <el-option :value="item"  v-for="(item,index) in directionArr" :key="index"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="录入人">
          <el-select size="mini" v-model="fromList.creator" placeholder="请选择">
            <el-option value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="题目备注">
          <el-input v-model="fromList.remarks" size="mini"></el-input>
        </el-form-item>
        <el-form-item label="企业简称">
          <el-input v-model="fromList.shortName" size="mini"></el-input>
        </el-form-item>
        <el-form-item label="城市">
          <el-col :span="12">
            <el-select size="mini" class="select_INT" v-model="fromList.province" placeholder="请选择">
              <el-option :value="item.city" v-for="(item,index) in cityArr" :key="index"></el-option>
            </el-select>
          </el-col>
          <el-col :span="12">
            <el-select size="mini" class="select_INT" v-model="fromList.city" placeholder="请选择">
              <el-option value="1"></el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item>
          <el-button>清除</el-button>
          <el-button @click="searchFn" type="primary">搜索</el-button>
        </el-form-item>
      </el-form>
      <div class="icon-text el-alert">
        <i class="el-alert__icon el-icon-info"></i>
        <span>数据一共 {{total}} 条</span>
      </div>
    <!-- 表格部分 -->
    <el-table :data="questionList" style="width: 100%">
      <el-table-column prop="number" label="试题编号" width="180">
      </el-table-column>
      <el-table-column prop="subject" label="学科" width="180">
      </el-table-column>
      <el-table-column prop="catalog" label="目录">
      </el-table-column>
      <el-table-column prop="questionType" label="题型">
        <template v-slot="scope">
          {{scope.row.questionType==1?'单选':'多选'}}
        </template>
      </el-table-column>
      <el-table-column prop="question" label="题干">
        <template v-slot='scope'>
          <span v-html='scope.row.question'>
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="addDate" label="录入时间" width="180">
        <template #default="{row}">
        {{ $dataFormat(row.addDate) }}
        </template>
      </el-table-column>
      <el-table-column prop="difficulty" label="难度">
        <template v-slot="scope">
          {{scope.row.difficulty==1?'简单':'困难'}}
        </template>
      </el-table-column>
      <el-table-column prop="creator" label="录入人">
      </el-table-column>
      <el-table-column label="操作" min-width="150px">
        <template v-slot="scope">
          <el-button  title="预览" @click="previewFn(scope.row)" type="primary" icon="el-icon-view" plain circle></el-button>
          <el-button title="修改" @click="changeFn(scope.row.id)" type="primary" icon="el-icon-edit" plain circle></el-button>
          <el-button title="删除" @click="removeFn(scope.row.id)" type="danger" icon="el-icon-delete" plain circle></el-button>
          <el-button title="加入精选" @click="addFn(scope.row.id)" type="success" icon="el-icon-check" plain circle></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="block">
      <el-pagination background layout=" prev, pager, next, sizes, jumper" @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[5, 10,15]" :current-page="query.page" :page-size="query.pagesize" :total="total">
      </el-pagination>
    </div>
    <!-- 题目预览对话框 -->
      <QuestDlog :previewArr="questionPreviewArr"
      :dialogVisible.sync="dialogVisible"
       @close="dialogVisible = false"></QuestDlog>
    </div>
  </el-card>
  </div>
</template>

<script>
// 题型  难度  方向
import { questionType, difficulty, direction } from '@/api/hmmm/constants.js'
// 城市数据
// import { provinces } from '@/api/hmmm/citys.js'
import { list, detail, remove, choiceAdd } from '@/api/hmmm/questions.js'
// 学科简单列表
import { simple } from '@/api/hmmm/subjects.js'
// 二级目录列表
import { simple as _simple } from '@/api/hmmm/directorys.js'
// 题目预览
import QuestDlog from '@/module-hmmm/components/questions-preview.vue'
export default {
  components: {
    QuestDlog
  },
  data () {
    return {
      subjectiD: '', // 搜索学科id
      query: {
        page: 1, // 页码
        pagesize: 10 // 每页条数
      },
      total: 0, // 数据总数
      user: '',
      questionList: [],
      // 表单数据
      fromList: {
        keyword: '', // 关键字1
        tags: '', // 标签
        subjectId: '', // 学科
        label: '', // 二级目录
        questionType: '', // 试题类型
        difficulty: '', // 难度
        direction: '', // 放向
        creator: '', // 录入人
        remarks: '', // 题目备注
        shortName: '', // 企业简称
        province: '', // 所在省份
        city: '' // 所在城市
      },
      userName: {
        subjectName: ''
      },
      // 学科列表
      subjectArr: [],
      // 二级目录列表
      directoryArr: [],
      dialogVisible: false, // 控制对话框显示隐藏
      questionPreviewArr: {}, // 弹框组件数据列表
      removeid: {
        // 删除图库id
        id: ''
      },
      cityArr: [], // 城市列表
      questionTypeArr: [], // 题型
      difficultyArr: [], // 难度
      directionArr: [] // 方向
    }
  },
  created () {
    // 基本图库列表
    this.questionListFn()
    // 学科简单列表
    this.subjectFn()
    // 二级目录
    this.directoryFn()
    // 城市列表
    // this.cityArr = provinces()
    // 题型
    this.questionTypeArr = questionType
    // 难度
    this.difficultyArr = difficulty
    // 方向
    this.directionArr = direction
  },
  methods: {
    handleSizeChange (newSize) {
      this.query.pagesize = newSize
      this.questionListFn()
    },
    subject (value) {
      // console.log(value)
      this.directoryFn(value)
      // 学科改变的时候获取他的value做id参数
      this.query.subjectID = value
    },
    handleCurrentChange (newPage) {
      this.query.page = newPage
      this.questionListFn()
    },
    // 基本题库列表
    async questionListFn () {
      const res = await list(this.query)
      this.questionList = res.data.items
      this.total = res.data.counts
      // console.log(this.questionList)
    },
    // 学科简单列表
    async subjectFn () {
      const res = await simple(this.userName)
      this.subjectArr = res.data
    },
    // 二级目录列表
    async directoryFn (value) {
      const res = await _simple({ subjectID: value })
      this.directoryArr = res.data
    },
    // 控制弹框关闭和显示
    handleClose (done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    // 题目预览
    async previewFn (id) {
      this.dialogVisible = true
      // console.log(id)
      const res = await detail(id)
      this.questionPreviewArr = res.data
      // console.log(this.questionPreviewArr)
      // console.log(res)
    },
    // 基础题库删除
    async removeFn (id) {
      this.removeid.id = id
      const res = await remove(this.removeid)
      // console.log(res)
      this.$message.success('删除成功')
      this.questionListFn()
    },
    // 精选题库添加取消
    addFn (id) {
      this.$confirm('此操作将该题目加入精选, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await choiceAdd({ id: id, choiceState: 1 })
          // console.log(res)
          this.$message({
            type: 'success',
            message: '加入成功!'
          })
          this.questionListFn()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    // 新增试题按钮
    newFn () {
      this.$router.push('/questions/new')
    },
    // 基础题库修改按钮
    changeFn (id) {
      this.$router.push(`/questions/new?id=${id}`)
    },
    // 搜索按钮 - 仅学科
    searchFn () {
      // 基本图库列表
      const res = this.questionListFn(this.query)
      // console.log(res)
    },
    btn_kFn (Boll) {
      this.dialogVisible = Boll
    }
  }
}
</script>
<style scoped lang='scss'>
// .btn_wrapper {
//   background-color: #fff;
//   padding: 10px;
// }
// .btn_wrapper1 {
//   padding: 20px 10px;
// }
.heardText {
  font-size: 12px;
  color: pink;
  text-align: center;
}
.select_INT  {
  width: 108px !important;
  margin-right: 5px;
}
// .button_Left {
//   margin-left: 145px;
// }
  .el-input  {
  width: 220px;
}
.el-select {
   width: 220px;
}

.btn-col {
  margin-top: 10px;
}
.btn-col1 {
  margin-top: 5px;
}
.icon-text {
  background-color: #f4f4f5;
  color: #909399;
  font-size: 12px;
}
.el-alert {
  padding: 8px 16px;
}
.block {
  text-align: right;
  background-color: #fff;
}
.dialog-footer {
  float: right;
}
</style>
